package com.cyl.h5.service;

import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.act.domain.entity.CouponActivity;
import com.cyl.manager.act.domain.entity.CouponCategory;
import com.cyl.manager.act.mapper.CouponActivityMapper;
import com.cyl.manager.act.mapper.CouponCategoryMapper;
import com.cyl.manager.act.service.CouponCategoryService;
import com.cyl.manager.lbs.domain.LbsZbjl;
import com.cyl.manager.lbs.mapper.LbsZbjlMapper;
import com.cyl.manager.lbs.pojo.query.LbsZbjlQuery;
import com.cyl.manager.ums.domain.entity.Member;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.livesass.LiveVideoService;
import com.volcengine.model.livesaas.response.GetActivityAPIResponse;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class H5LbsZbjlService {
 
    @Autowired
    private LbsZbjlMapper lbsZbjlMapper;
    @Autowired
    private CouponActivityMapper couponActivityMapper;
    @Autowired
    private CouponCategoryMapper couponCategoryMapper;
    
    @Autowired
    private LiveVideoService liveVideoService;
    
    @Autowired
    private CouponCategoryService couponCategoryService;
    

 	
    public static void main(String[] args) {
		System.out.println(URLEncoder.encode(Base64.encode("QQ")));
	}
    public LbsZbjl getByZbjlId(Long zbjlId,long userid) {
    	  Member member =  (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
    	  LbsZbjl lbsZbjl = 	lbsZbjlMapper.selectById(zbjlId);
    	  QueryWrapper<CouponActivity> qw = new QueryWrapper<>();
	  	  qw.eq("qlx", 2);
	  	  qw.eq("zbjlid", lbsZbjl.getId());
	  	  CouponActivity couponActivity =  couponActivityMapper.selectOne(qw);
	  	  Map<String, Object> params = new HashMap<String, Object>();
		  long timestamp = System.currentTimeMillis();
	  	  params.put("couponActivity", couponActivity);
	  	  if(couponActivity!=null) {
		  	  params.put("couponCategory", couponCategoryService.selectCheckById(couponActivity.getCategoryid()));
	  	  }else {
	  		  params.put("couponCategory",null);
	  	  }
	  	  params.put("timestamp", timestamp);
	  	  if(member != null && member.getNickname()!=null) {
  	        params.put("nickname", URLEncoder.encode(Base64.encode(member.getNickname())));
          }
	  	  params.put("userid", member.getId());
	  	  params.put("sign", liveVideoService.getSign(zbjlId,userid,timestamp));

	  	  lbsZbjl.setParams(params); 
  	  
      return lbsZbjl;
    }
    
    
    
    /**
     * 查询直播列表列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 直播列表
     */
    public List<LbsZbjl> selectList(LbsZbjlQuery query) {
        QueryWrapper<LbsZbjl> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        Long qrDeptZbjId = query.getDeptZbjId();
        qw.eq("dept_zbj_id", qrDeptZbjId);
        qw.ge("create_time", query.getBeginTime());
        qw.lt("create_time", query.getEndTime());
        return lbsZbjlMapper.selectList(qw);
    }

}
